﻿using System.Collections.Generic;
using System.IO;
using Assets.Scripts.Handlers;
using Assets.Scripts.NetFrame.Events;
using ExitGames.Client.Photon;
using ProtoBuf;
using UnityEngine;
using WorldOfSuperpowers_COMMON;

namespace Assets.Scripts.NetFrame
{
    public class PhotonPeerManager:IPhotonPeerListener
    {
        private static PhotonPeerManager instance;

        private PhotonPeerManager()
        {

        }

        public static PhotonPeerManager GetInstance()
        {
            if (instance == null)
            {
                instance = new PhotonPeerManager();
            }
            return instance;
        }
        private PhotonPeer peer;
        public Dictionary<OperationCode,BaseHandler> handlerDic = new Dictionary<OperationCode, BaseHandler>();
        public Dictionary<EventCode,BaseEventHandler> eventHandlerDic = new Dictionary<EventCode, BaseEventHandler>();


        public PhotonPeer Init()
        {
            peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
            peer.Connect("127.0.0.1:4530", "WorldOfSuperpowers");
            return peer;
        }

        public void DebugReturn(DebugLevel level, string message)
        {

        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            Debug.Log("收到新的反馈消息:"+ (OperationCode)operationResponse.OperationCode);
            handlerDic[(OperationCode)operationResponse.OperationCode].Response(operationResponse);
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            switch (statusCode)
            {
                case StatusCode.Connect:
                    Debug.Log("连接成功");
                    break;
                case StatusCode.Disconnect:
                    Debug.LogError("断开连接");
                    break;
            }
        }

        public void OnEvent(EventData eventData)
        {
            Debug.Log("收到event事件:"+(EventCode)eventData.Code);
            eventHandlerDic[(EventCode)eventData.Code].Response(eventData.Parameters);
        }

        /// <summary>
        /// 只用发送标志枚举和实体类，本函数会自动转换实体类为Object，并添加到字典内发送给服务器
        /// </summary>
        /// <typeparam name="T">实体类泛型</typeparam>
        /// <param name="code">标志枚举</param>
        /// <param name="data">实体类</param>
        public void SendMessage<T>(OperationCode code,T data)
        {
            //todo message可以进入对象池管理
            Dictionary<byte,object> message = new Dictionary<byte, object>();
            object content = ProtoTranslater.GetInstance().GetBytesFromType(data);
            message.Add((byte)code, content);
            peer.OpCustom((byte)code, message, true);
        }

        /// <summary>
        /// 发送基础数据类型
        /// </summary>
        /// <param name="code">操作码</param>
        /// <param name="data">具体消息</param>
        public void SendMessage(OperationCode code,Dictionary<byte,object> data)
        {
            peer.OpCustom((byte) code, data, true);
        }
    }
}
